<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章ID" prop="articleId">
        <el-input
          v-model="queryParams.articleId"
          placeholder="请输入文章ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      
      <el-form-item>
        <el-button type="info" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['blog:article_comments:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:article_comments:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
       row-key="id"
       v-loading="loading" 
       :data="article_commentsList" 
       @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="文章标题" align="center" prop="title" show-overflow-tooltip>
        <template #default="scope">
          <el-link  :underline="false" @click="handleToArticle(scope.row)">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="被回复的用户名称" align="center" prop="parentNickName" >
        <template #default="scope">{{ scope.row.parentNickName || '无' }}</template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" >
        <template #default="scope">
            <div v-html="scope.row.content" />
        </template>
      </el-table-column>
      <el-table-column label="是否通过审核" align="center" prop="isApproved" >
        <template #default="scope">
               <el-switch
                  v-model="scope.row.isApproved"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
               ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="info"  @click="handleInfo(scope.row)" v-hasPermi="['blog:article_comments:list']">详情</el-button>
          <el-button link type="info"  @click="handleUpdate(scope.row)" v-hasPermi="['blog:article_comments:edit']">修改</el-button>
          <el-button link type="info"  @click="handleDelete(scope.row)" v-hasPermi="['blog:article_comments:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="article_commentsRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="文章编号 " prop="articleId">
          <el-input v-model="form.articleId" placeholder="请输入文章" />
        </el-form-item>
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="父评论编号" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父评论" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="点赞数" prop="likes">
          <el-input type="number" v-model="form.likes" placeholder="请输入点赞数" />
        </el-form-item>
        <el-form-item label="是否通过" prop="isApproved">
          <el-select v-model="form.isApproved" placeholder="请选择是否通过审核">
            <el-option 
               v-for="dict in comment_approved" 
               :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看详情 -->
    <div class="box-drawer"> 
      <el-drawer
        class="remark-drawer"
        v-model="infoOpen"
        size="50%"
      >
      <template #header="{ close, titleId, titleClass }">
          <div :id="titleId" :class="titleClass">
            <span style="margin-right:20px;font-size: 18px;color:black;">{{ form.title }}</span>
            <span class="item-font">{{ form.createTime }}</span>&nbsp;
            
          </div>
        </template>
        <el-descriptions  
           :column="1"  
           border 
           label-width="100px" 
           class="my-descriptions"
        >
         <el-descriptions-item label="评论人编号" label-align="center" align="center">
            <span >{{ form.userId }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="评论人" label-align="center" align="center">
            <span >{{ form.nickName }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="文章编号" label-align="center" align="center">
            <span >{{ form.articleId }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="文章标题" label-align="center" align="center">
            <span >{{ form.title }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="评论内容" label-align="center" align="center">
            <span v-html="form.content"></span>
          </el-descriptions-item>
          <el-descriptions-item label="点赞数" label-align="center" align="center">
            <span >{{ form.likes }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="是否通过" label-align="center" align="center">
            <el-tag v-if="form.isApproved === '0'" type="success">通过</el-tag>
          <el-tag v-else type="danger">未通过</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间" label-align="center" align="center">
            <span >{{ form.createTime }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="根评论编号" label-align="center" align="center">
            <span >{{ form.rootId }}</span>
          </el-descriptions-item>
        </el-descriptions>
      </el-drawer>
    </div>
  </div>
</template>

<script setup name="Article_comments">
import { listArticle_comments, getArticle_comments, delArticle_comments, addArticle_comments, updateArticle_comments } from "@/api/blog/article_comments";

const { proxy } = getCurrentInstance();
const { comment_approved } = proxy.useDict("comment_approved");

const article_commentsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    articleId: null,
    userId: null,
    parentId: null,
    parentUserId: null,
    content: null,
    isApproved: null,
  },
  rules: {
    articleId: [
      { required: true, message: "文章ID", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户ID users.id不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "评论内容，不能为空不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 文章是否通过审核 */
/** 账号状态修改 */
function handleStatusChange(row) {
  let text = row.isApproved === "0" ? "通过" : "不通过";
  proxy.$modal.confirm('确认要"' + text + '""' + row.id + '"评论吗?').then(function () {
    let params = { id: row.id, isApproved: row.isApproved };
    return updateArticle_comments(params);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.isApproved = row.isApproved === "0" ? "1" : "0";
  });
}


/** 转到文章详情 */
function handleToArticle(row) {
  proxy.$router.push({ path: '/blog/blog_articles', query: { id: row.articleId } });
}

/** 查看文件详情 */
const infoOpen = ref(false);
function handleInfo(row) {
  infoOpen.value = true;
  reset();
  form.value = row
   
}

/** 查询评论管理列表 */
function getList() {
  loading.value = true;
  listArticle_comments(queryParams.value).then(response => {
    article_commentsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    articleId: null,
    userId: null,
    parentId: null,
    parentUserId: null,
    content: null,
    likes: null,
    isApproved: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("article_commentsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评论管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getArticle_comments(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改评论管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["article_commentsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle_comments(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle_comments(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除评论管理编号为"' + _ids + '"的数据项？').then(function() {
    return delArticle_comments(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('blog/article_comments/export', {
    ...queryParams.value
  }, `article_comments_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped lang="scss">
.box-drawer {
  &:deep(.el-drawer.remark-drawer) {
    .el-drawer__header {
      margin-bottom: 0px !important;
    }
    
  }
}


</style>